﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;

namespace Xacml.Core.Runtime.Functions
{
  public class AnyOfAny : FunctionBase
  {
    public override string Id
    {
      get
      {
        return "urn:oasis:names:tc:xacml:1.0:function:any-of-any";
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[3]
        {
          (IDataType) DataTypeDescriptor.Function,
          (IDataType) DataTypeDescriptor.Bag,
          (IDataType) DataTypeDescriptor.Bag
        };
      }
    }

    public override IDataType Returns
    {
      get
      {
        return (IDataType) DataTypeDescriptor.Boolean;
      }
    }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      IFunction function = args[0].GetFunction(0);
      foreach (object obj1 in args[1].Elements)
      {
        foreach (object obj2 in args[2].Elements)
        {
          EvaluationValue evaluationValue = function.Evaluate(context, (IFunctionParameter) new EvaluationValue(obj1, args[1].GetType(context)), (IFunctionParameter) new EvaluationValue(obj2, args[2].GetType(context)));
          if (evaluationValue.BoolValue)
            return evaluationValue;
        }
      }
      return EvaluationValue.False;
    }
  }
}
